package homework02.exercise3;
/*
* 专利文档中不仅有IPC分类号，还有CPC分类号，CPC的格式与IPC类似，
* 但多了两个字段position和value，这两个字段取值为单个字符，请重写Ipc类和Cpc类，
* 并增加一个父类Classification。
* */
public class Classification {

    final char SEPARATOR = '/';
    private String section; //部
    private String class_;  //大类
    private String subclass;    //小类
    private String mainGroup;   //大组
    private String subgroup;    //小组
    private String position = "1";
    private char value = 'a';
    private int pos;

    private String Classification;

    //含有五个参数的构造器（分别为部、大类、小类、大组和小组）
    public Classification(String section, String class_, String subclass,
               String mainGroup, String subgroup) {
        this.section = section;
        this.class_ = class_;
        this.subclass = subclass;
        this.mainGroup = mainGroup;
        this.subgroup = subgroup;
    }


    public char getsection() {
        return Classification.charAt(0);
    }

    public Integer getclass() {
        return Integer.parseInt(Classification.substring(1, 3));
    }

    public char getsubclass() {
        return Classification.charAt(3);
    }

    public Integer getPos() {
        int pos = Classification.lastIndexOf(SEPARATOR);
        return Classification.indexOf(SEPARATOR);
    }

    public String getmaingroup() {
        int startIndex = Classification.indexOf("55");
        return Classification.substring(startIndex, startIndex + 2);
    }

    public String getsubgroup() {
        int slashIndex = Classification.indexOf('/');
        return Classification.substring(slashIndex + 1);
    }

    //toString()函数将这五部分拼接成IPC
    @Override
    public String toString() {

        return "IPC号为: " + section +
                class_.format("%2s", class_).replace(' ', '0') + subclass
                + mainGroup.format("%4s", mainGroup) + SEPARATOR + subgroup;
    }


    public String getCPC() {
        return  section +
                class_.format("%2s", class_).replace(' ', '0') + subclass
                + mainGroup.format("%4s", mainGroup) + SEPARATOR + subgroup + position + value;
    }

}
